// 测试房主和其他玩家的颜色分配
console.log('👑 测试房主和其他玩家颜色分配...')

// 尝试多种方式访问游戏状态
let gameStore = null
let players = []
let currentPlayer = null

// 方法1: 通过 window 对象
if (window.gameStore) {
  gameStore = window.gameStore
  console.log('✅ 通过 window.gameStore 访问')
} 
// 方法2: 通过 Vue 应用实例
else if (window.__VUE_APP__) {
  const app = window.__VUE_APP__
  if (app._instance && app._instance.provides) {
    gameStore = app._instance.provides.gameStore
    console.log('✅ 通过 Vue 应用实例访问')
  }
}
// 方法3: 通过 document 查找
else {
  // 尝试从页面元素中获取信息
  console.log('🔍 尝试从页面元素获取信息...')
  
  // 查找玩家列表元素
  const playerElements = document.querySelectorAll('[class*="player"], [class*="Player"]')
  console.log('找到玩家元素:', playerElements.length)
  
  // 查找颜色显示元素
  const colorElements = document.querySelectorAll('[style*="background-color"]')
  console.log('找到颜色元素:', colorElements.length)
  
  // 尝试从 localStorage 获取
  const storedData = localStorage.getItem('gameState') || localStorage.getItem('pinia')
  if (storedData) {
    console.log('从 localStorage 找到数据:', storedData.substring(0, 100) + '...')
  }
  
  // 尝试从 sessionStorage 获取
  const sessionData = sessionStorage.getItem('gameState') || sessionStorage.getItem('pinia')
  if (sessionData) {
    console.log('从 sessionStorage 找到数据:', sessionData.substring(0, 100) + '...')
  }
}

if (gameStore) {
  console.log('✅ GameStore 可用')
  
  players = gameStore.players || []
  currentPlayer = gameStore.currentPlayer
  
  if (players && players.length > 0) {
    console.log('\n=== 玩家颜色分配 ===')
    
    // 找到房主
    const owner = players.find(p => p.isOwner)
    const otherPlayers = players.filter(p => !p.isOwner)
    
    if (owner) {
      console.log(`👑 房主: ${owner.nickname}`)
      console.log(`   头像颜色: ${owner.avatarColor}`)
      console.log(`   画笔颜色: ${owner.brushColor}`)
    }
    
    if (otherPlayers.length > 0) {
      console.log('\n👥 其他玩家:')
      otherPlayers.forEach((player, index) => {
        console.log(`   ${index + 1}. ${player.nickname}`)
        console.log(`      头像颜色: ${player.avatarColor}`)
        console.log(`      画笔颜色: ${player.brushColor}`)
      })
    }
    
    // 检查颜色唯一性
    const allBrushColors = players.map(p => p.brushColor).filter(c => c)
    const uniqueBrushColors = new Set(allBrushColors)
    
    console.log('\n=== 颜色唯一性检查 ===')
    console.log(`总玩家数: ${players.length}`)
    console.log(`画笔颜色数: ${allBrushColors.length}`)
    console.log(`唯一颜色数: ${uniqueBrushColors.size}`)
    
    if (allBrushColors.length === uniqueBrushColors.size) {
      console.log('✅ 所有玩家都有不同的画笔颜色')
    } else {
      console.log('❌ 有重复的画笔颜色')
      console.log('所有画笔颜色:', allBrushColors)
    }
    
    // 检查房主和其他玩家是否使用不同颜色
    if (owner && otherPlayers.length > 0) {
      const ownerBrushColor = owner.brushColor
      const otherBrushColors = otherPlayers.map(p => p.brushColor)
      
      const hasConflict = otherBrushColors.includes(ownerBrushColor)
      
      if (!hasConflict) {
        console.log('✅ 房主和其他玩家使用不同的画笔颜色')
      } else {
        console.log('❌ 房主和其他玩家使用了相同的画笔颜色')
        console.log(`房主画笔颜色: ${ownerBrushColor}`)
        console.log(`其他玩家画笔颜色: ${otherBrushColors.join(', ')}`)
      }
    }
    
    // 显示当前玩家信息
    if (currentPlayer) {
      console.log('\n=== 当前玩家信息 ===')
      console.log(`昵称: ${currentPlayer.nickname}`)
      console.log(`是否房主: ${currentPlayer.isOwner ? '是' : '否'}`)
      console.log(`头像颜色: ${currentPlayer.avatarColor}`)
      console.log(`画笔颜色: ${currentPlayer.brushColor}`)
    }
    
  } else {
    console.log('❌ 没有玩家')
  }
} else {
  console.log('❌ GameStore 不可用')
  console.log('💡 请确保：')
  console.log('   1. 已经进入游戏页面')
  console.log('   2. 已经创建或加入房间')
  console.log('   3. 页面完全加载完成')
  
  // 显示当前页面信息
  console.log('\n=== 页面信息 ===')
  console.log('当前URL:', window.location.href)
  console.log('页面标题:', document.title)
  
  // 检查是否有游戏相关的元素
  const gameElements = document.querySelectorAll('[class*="game"], [class*="Game"], [class*="room"], [class*="Room"]')
  console.log('游戏相关元素数量:', gameElements.length)
  
  // 检查是否有画布
  const canvasElements = document.querySelectorAll('canvas')
  console.log('画布元素数量:', canvasElements.length)
}

console.log('\n测试完成')
